package org.massdeception.content.servlets.authors;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.massdeception.content.entities.PMF;
import org.massdeception.content.entities.Slide;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;


public class AuthorEditSlideServlet extends AuthorServlet 
{
    private static final Logger log = Logger.getLogger(AuthorSelectSlidesServlet.class.getName());
    
    public void process(HttpServletRequest request, HttpServletResponse response, User user)
        throws IOException, ServletException
    {
        try
        {
            String slideId = request.getParameter("slideId");
            if ( (slideId == null) || ("".equals(slideId)) )
            {
                response.sendError(500, "Invalid slide Id specified, please try again.");
                log.log(Level.WARNING, "Invalid slide Id specified.");
                return;
            }

            Key key = KeyFactory.createKey(Slide.class.getSimpleName(), Long.parseLong(slideId));
            Slide slide = PMF.get().getPersistenceManager().getObjectById(Slide.class, key);
            
            request.setAttribute("slide", slide);
            
            // forward the request
            RequestDispatcher dispatcher = request.getRequestDispatcher("/authorUploadSlideEdit.jsp");
            dispatcher.forward(request, response);
            
            return;
        }
        catch (Throwable t)
        {
            response.sendError(500, "Request could not be processed.");
            log.log(Level.SEVERE, "Request could not be processed.", t);
            return;
        }
        
    }
}